package cn.gok.util.lambda;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.function.*;

/**
 * @ClassName 常用函数接口
 * @Description TODO
 * @Author HP
 * @Date 15:25 2022/7/11
 * @Version 1.0
 **/
class Student{
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public class 常用函数接口 {
    public static void main(String[] args) {
//        Runnable接口();
//        doRun(()->{
//            int y =1;
//            System.out.println("!!!!");
//        });
//        Consumer接口();
       // Supplier接口();
        //Function接口();
        //简化lambda();
        Predicate接口();

    }

    private static void Predicate接口() {
        Predicate<Integer> predicate = (i)->{return i>=100;};
        System.out.println(predicate.test(99));
        System.out.println(predicate.test(101));

        Predicate<Integer> predicate1 = i->i%2==0;
        System.out.println(predicate.and(predicate1).test(102));
        System.out.println(predicate.or(predicate1).test(102));




    }

    private static void 简化lambda() {
        String str = "sadada";
        Consumer<String> con = System.out::println;
        Function<String,Integer> function = Integer::parseInt;
//        Function<String,String> function1 = (n)->{return str.substring(n) }
        Function <Integer,String> function2 = str::substring;
        System.out.println(function2.apply(2));

        //BiFunction<String,Integer,Student> function1 = (name,age)->{return new Student(name,age);};
        BiFunction<String,Integer,Student> function1 = Student::new;
        System.out.println(function1.apply("asd",1223));
    }


    private static void Function接口() {
        Function<String,Integer> function = (str)-> str.length();
        System.out.println(function.apply("aadasdasdasdasdasdda"));
        Function<Integer,Integer> function1=i->i+100;
        System.out.println(function1.compose(function).apply("我是asda"));

        Function<Float,Float> floatFunction = i->i*i;
        Function<Float,Float> floatFloatFunction = i ->i*100;
        System.out.println(floatFloatFunction.compose(floatFunction).apply(16f));

    }



    private static void Supplier接口() {
        Supplier<String> sup =()->new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        System.out.println(sup.get());
    }

    private static void Consumer接口() {
        Consumer<String> con1 =(String str)->{
            str = "!";
            System.out.println(str);
        };
        Consumer<String> con2 =(String str)->{
            str = "!";
            System.out.println(str);
        };
        con1.accept("abc");
        con1.andThen(con2).accept("abc");
    }

    private static void doRun(Runnable run){
        run.run();
    }
    private static void Runnable接口() {
        int x = 100;
        Runnable r =new Runnable() {
            @Override
            public void run() {
                System.out.println("!!!!"+x);
            }
        };
        r.run();
    Runnable r1 = ()->{
        int y =1;
        System.out.println("!!!!"+x);
    };
    r1.run();

    }


















}
